package edu.ccut.saturn.studio.wfg.command;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;

import org.eclipse.gef.commands.Command;

import edu.ccut.saturn.studio.wfg.model.Shape;
import edu.ccut.saturn.studio.wfg.model.ShapesDiagram;

public class ShapeCreateCommand extends Command {

	private static final String SHAPE_CREATE_COMMAND_SHAPE_CREATION = "shape creation";
	private Shape newShape;
	private final ShapesDiagram parent;
	private Rectangle bounds;

	public ShapeCreateCommand(Shape newShape, ShapesDiagram parent,
			Rectangle bounds) {
		this.newShape = newShape;
		this.parent = parent;
		this.bounds = bounds;
		setLabel(SHAPE_CREATE_COMMAND_SHAPE_CREATION);
	}

	public boolean canExecute() {
		return newShape != null && parent != null && bounds != null;
	}

	public void execute() {
		newShape.setLocation(bounds.getLocation());
		Dimension size = bounds.getSize();
		if (size.width > 0 && size.height > 0)
			newShape.setSize(size);
		redo();
	}

	public void redo() {
		parent.addShape(newShape);
	}

	public void undo() {
		parent.removeShape(newShape);
	}
}